local TILE_SAND 		= 	9059
local TUMB_ENTRANCE		= 	1345
local MUD_HOLE		=	489
local duration = 5 * 60000 -- 5 minutes

local sand = {231, 9059, 8315, 8316, 8317, 8318, 8319, 8320, 8321, 8322} -- 836
local dirt = {103, 351, 352, 353, 354, 355, 804, 806, 4832, 4833, 7352, 9021, 9022, 9023, 9024, 9025, 8594}

local function __doTransformHole__(parameters)
	local thing = getTileItemById(parameters.pos, MUD_HOLE)
	local newItem = doTransformItem(thing.uid, parameters.oldType)
	--- jedna linijka?
	--- doTransformItem(getTileItemById(parameters.pos, MUD_HOLE).uid, parameters.oldType)
end

function onUse(cid, item, frompos, item2, topos)
	if (isInArray(CLOSED_HOLE, item2.itemid) ) then
		if item2.itemid == 8579 then
			doTransformItem(item2.uid, 8585)
		else
			doTransformItem(item2.uid, item2.itemid + 1)
		end
	elseif (item2.itemid == TILE_SAND) and (item2.actionid == TUMB_ENTRANCE) then
		if (math.random(1, 5) == 1) then
			doTransformItem(item2.uid, MUD_HOLE)
			addEvent(__doTransformHole__, duration, {oldType = item2.itemid, pos = topos})
		end
		doSendMagicEffect(topos, CONST_ME_POFF)
	elseif(isInArray(sand, item2.itemid)) then --- add sand
		doPlayerAddItem(cid, 9675, 1, true)
		doSendMagicEffect(topos, CONST_ME_POFF)
	elseif (isInArray(dirt, item2.itemid)) then --- add worm 
		if math.random(100) <= 50 then
			-- doPlayerAddItem(cid, 3976, 1, true)
			doCreateItem(3976, 1, topos)
		end
		doSendMagicEffect(topos, CONST_ME_POFF)
	else
		return false
	end

	doDecayItem(item2.uid)
	return true
end
